package com.lecture.essence.demo04;

import cn.hutool.core.util.ArrayUtil;
import com.lecture.essence.demo04.FXNewsBean;
import com.lecture.essence.demo04.IFXNewsListener;
import com.lecture.essence.demo04.IFXNewsPersister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class FXNewsProvider {

    @Autowired
    private IFXNewsListener newsListener;

    @Autowired
    private IFXNewsPersister newsPersister;

    public FXNewsProvider(IFXNewsListener newsListener, IFXNewsPersister newsPersister) {
        this.newsListener = newsListener;
        this.newsPersister = newsPersister;
    }

    public void getAndPersistNews() {
        String[] newsIds = newsListener.getAvailableNewsIds();
        if (ArrayUtil.isEmpty(newsIds)) {
            return;
        }
        for (String newsId : newsIds) {
            System.out.println("newsId：" + newsId);
            FXNewsBean newsBean = newsListener.getNewsByPK(newsId);
            newsPersister.persistNews(newsBean);
            newsListener.postProcessIfNecessary(newsId);
        }
    }

}
